﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Facade.Implementations;
using BackEnd.Facade.Interfaces;
using WebMVC.Areas.AdminPanel.Models;
using Domain;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class TaxiServiceController : AdminControllerBase
    {
        protected ITaxiServiceManager serviceManager;

        public TaxiServiceController()
            : this(new TaxiServiceManager())
        {
        }

        public TaxiServiceController(ITaxiServiceManager manager)
        {
            serviceManager = manager;
        }
        //
        // GET: /AdminPanel/TaxiService/

        public ActionResult Index()
        {
            var services = serviceManager.GetAllServices();
            var models = new List<TaxiServiceListViewModel>();

            foreach (var serv in services)
            {
                models.Add(new TaxiServiceListViewModel() {
                    NameEN = serv.NameEN,
                    Id = serv.Id,
                    CreateDate = serv.CreateDate,
                    HasMeter = serv.HasMeter,
                    MinPrice = serv.PricePolicy.MinPrice,
                    PricePerKM = serv.PricePolicy.PricePerKM,
                    MinPriceKM = serv.PricePolicy.MinPriceKM,
                    NoSmoking = serv.NoSmoking,
                    Conditioning = serv.Conditioning,
                    PrimaryPhoneNumber = serv.Contacts.PhoneNumber,
                    Status = serv.Status.Code
                });
            }


            return View(models);
        }

        //
        // GET: /AdminPanel/TaxiService/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminPanel/TaxiService/Create

        public ActionResult Create()
        {
            var model = new TaxiServiceCreateViewModel()
            {
                Cars = GetAllCars(),
                Districts = GetAllDistricts()
            };
            return View(model);
        } 

        //
        // POST: /AdminPanel/TaxiService/Create

        [HttpPost]
        public ActionResult Create(TaxiServiceCreateViewModel vmodel)
        {
            if (ModelState.IsValid)
            {
                var service = new TaxiService()
                {
                    NameAM = vmodel.NameAM,
                    NameEN = vmodel.NameEN,
                    NameRU = vmodel.NameRU,
                    DescriptionEN =vmodel.DescriptionEN,
                    DescriptionAM = vmodel.DescriptionAM,
                    DescriptionRU = vmodel.DescriptionRU,
                    PricePolicy = new PricePolicy(){
                        MinPrice = vmodel.MinPrice.Value,
                        PricePerKM = vmodel.PricePerKM.Value,
                        MinPriceKM = vmodel.MinPriceKM,
                        WaitMinutePrice = vmodel.WaitMinutePrice,
                        FreeWaitMinutes = vmodel.FreeWaitMinutes,
                        UpTo6KMPrice = vmodel.UpTo6KMPrice,
                        OutsideTownPrice = vmodel.OutsideTownKMPrice,
                        ToAirportPrice = vmodel.ToAirportPrice,
                    },

                    Conditioning = vmodel.Conditioning,
                    HasMeter = vmodel.HasMeter,
                    NoSmoking = vmodel.NoSmoking,
                    SpeaksEnglish = vmodel.SpeaksEnglish,

                    ServingFromYear = vmodel.ServingFromYear,
                    ParkQuantity = vmodel.ParkQuantity,
                    Contacts = new ContactInfo(){
                        PhoneNumber = vmodel.PrimaryPhoneNumber,
                        PhoneNumber1 = vmodel.PhoneNumber1,
                        PhoneNumber2 = vmodel.PhoneNumber2,
                        PhoneNumber3 = vmodel.PhoneNumber3,
                        PhoneNumber4 = vmodel.PhoneNumber4,
                    },
                    District = vmodel.DistrictId.HasValue ? new District(){Id = vmodel.DistrictId.Value} : null
                };

                service.Transports = new List<ServiceTransport>();

                foreach (var carId in vmodel.SelectedCarIds)
                {
                    service.Transports.Add(new ServiceTransport()
                    {
                        Car = new Car() { Id = carId },
                    });
                }
                
                int newId = serviceManager.CreateService(service);
                Info = String.Format("Taxi Service \"{0}\" created with ID: {1}", vmodel.NameEN, newId);
                return RedirectToAction("Index");
            }
            else
            {
                vmodel.Districts = GetAllDistricts();
                vmodel.Cars = GetAllCars();
                return View(vmodel);
            }
        }
        
        //
        // GET: /AdminPanel/TaxiService/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AdminPanel/TaxiService/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminPanel/TaxiService/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminPanel/TaxiService/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private List<SelectListItem> GetAllDistricts()
        {
            var dists = serviceManager.GetAllDistricts();

            var result = new List<SelectListItem>();
            result.Add(new SelectListItem() { 
                Text = "All",
                Value = "",
                Selected = true
            });
            foreach (var distDo in dists)
            {
                result.Add(new SelectListItem(){
                    Text = distDo.NameEN,
                    Value = distDo.Id.ToString()
                });
            }
            return result;
        }

        private List<SelectListItem> GetAllCars()
        {
            var cars = serviceManager.GetAllCars();

            var result = new List<SelectListItem>();
            
            foreach (var carDo in cars)
            {
                result.Add(new SelectListItem()
                {
                    Text = carDo.Make + " " + carDo.Model,
                    Value = carDo.Id.ToString()
                });
            }
            return result;
        }
    }
}
